import { Request } from '../../wxParse/request.js';
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        statusBarHeight: getApp().globalData.statusBarHeight,
        shopname: '',
        weixin: 'pmliuyang003',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        console.log(app);
        this.activitId = options.activitId; //活动id
        // 登录
        wx.login({
            success: function (res) {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                if (res.code) {
                    wx.request({
                        url: 'https://weizhi.darenjian.com/api/pinduo/chou_award/login/login',
                        method: 'POST',
                        data: {
                            code: res.code,
                        },
                        header: {
                            'content-type': 'application/x-www-form-urlencoded',
                            cookie: 'PHPSESSID=' + 1,
                        },
                        success: function (res) {
                            console.log(res.data.loginCode);
                            if (res.data.loginCode == 1) {
                                app.globalData.loginStatus = true; //授权登录
                                app.globalData.openid = res.data.openid;
                                app.globalData.userid = res.data.userId; //用户id
                                app.globalData.userName = res.data.userName; //用户名
                            } else {
                                app.globalData.loginStatus = false; //未授权登录
                                app.globalData.openid = res.data.openid;
                            }

                            this.GetAwardInfo(); //加载活动详情信息
                            this.GetUserAddresInfo();   //获取用户的 数据库的地址信息
                            app.globalData.status = res.data.status; //用户登录状态
                            this.setData({
                                loginStatus: app.globalData.loginStatus,
                            });
                            console.log(app);
                        }.bind(this),
                    });
                } else {
                    console.log('登录失败！' + res.errMsg);
                }
            }.bind(this),
        });
    },
    /**
     * 获取商品详情信息
     */
    GetAwardInfo: function (param) {
        new Request('Prize/ShowPrizeInfo').request(
            () => {
                return {
                    openid: app.globalData.openid, //用户openid
                    userId: app.globalData.userid, //用户userid
                    ActivityId: this.activitId, //活动id
                };
            },
            (res) => {
                console.log(res.data);
                if (res.data.data) {
                    this.setData({
                        awardInfo: res.data.data, //活动消息
                        shopname: res.data.data.award_name, //商品名字
                        shop_good: res.data.data.award_image, //商品图片
                    });
                }
            }
        );
    },
    /**
     * 获取用户 数据库地址
     */
    GetUserAddresInfo: function () {
        //查询获取用户地址 如果用户已经在数据库中有记录 则不需要弹窗询问用户选择地址 直接赋值到输入框上
        new Request('User/GetUserAddressInfo').request(
            () => {
                return {
                    openid: app.globalData.openid, //用户openid
                };
            },
            (res) => {
                if (res.data.code == 2) {
                    this.setData({
                        phone: res.data.data.userPhone,
                        address: res.data.data.userAddress,
                    });
                    this.address = res.data.data.userAddress;
                    this.phone = res.data.data.userPhone;
                    this.openGetUserAddress = false;
                } else if (res.data.code == 4) {
                    //用户第一次输入地址 需要开启 获取地址的授权
                    this.openGetUserAddress = true;
                }
            }
        );
    },
    /**
     * 点击复制 微信号
     */
    CopyWxHao: function () {
        wx.setClipboardData({
            data: 'pmliuyang003',
            success(res) {
            },
        });
    },
    /**
     * 获取手机输入框内容
     */
    _bindinputPhone: function (param) {
        this.phone = param.detail.value;
    },
    /**
     * 获取地址输入框内容
     */
    _bindinputAddress: function (param) {
        this.address = param.detail.value;
    },
    /**
     * 上传奖品地址
     */
    UpdatePrizeAddresInfo: function (param) {
        if (!this.openGetUserAddress) {
            if (this.phone != '' && this.address != '' && this.address != undefined && this.phone != undefined) {
                new Request('Prize/SavePrizeAddress').request(
                    () => {
                        return {
                            openid: app.globalData.openid, //用户openid
                            userId: app.globalData.userid, //用户userid
                            ActivityId: this.activitId, //活动id
                            addressText: this.address,
                            phone: this.phone,
                        };
                    },
                    (res) => {
                        // if (res.data.data == 1) {
                            this.Update = true;
                            wx.showToast({
                                title: '成功',
                                icon: 'success',
                                duration: 2000,
                            });
                            setTimeout(()=>{
                                wx.switchTab({
                                    url: '/pages/home/home',
                                });
                            },2001);
                        // }
                    }
                );
            } else {
                wx.showToast({
                    title: '上填写完整的地址和手机号',
                    icon: 'none',
                    duration: 2000,
                });
            }
        } else {
            wx.chooseAddress({
                success: (res) => {
                    if (res.telNumber || res.cityName || res.countyName || res.detailInfo) {
                        this.phone = res.telNumber;
                        this.address = res.provinceName + res.cityName + res.countyName + res.detailInfo;
                        this.setData({
                            phone: this.phone,
                            address: this.address,
                        });
                    }
                    console.log(res);
                },
            });
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000,
            });
            if(this.phone != undefined && this.phone != '' && this.address != undefined && this.address != ''){
                this.openGetUserAddress = false;
            }

        }
    },
    /**
     * 返回首页
     */
    gohome: function (param) {
        new Request('Prize/GetPrizeAddressInfo').request(
            () => {
                return {
                    openid: app.globalData.openid, //用户openid
                    userId: app.globalData.userid, //用户userid
                    ActivityId: this.activitId, //活动id
                };
            },
            (res) => {
                if (res.data.data == -2) {
                    wx.showModal({
                        title: '温馨提示',
                        content: '未填写收货信息是否返回首页',
                        success(res) {
                            if (res.confirm) {
                                console.log('用户点击确定');
                                wx.switchTab({
                                    url: '/pages/home/home',
                                });
                            } else if (res.cancel) {
                                wx.showToast({
                                    title: '已取消',
                                    icon: 'none',
                                    duration: 2000,
                                });
                            }
                        },
                    });
                } else {
                    wx.switchTab({
                        url: '/pages/home/home',
                    });
                }
            }
        );
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
});
